package com.example.featuresdk.database
//
//import android.content.Context
//import androidx.room.Room
//import androidx.room.RoomDatabase
//import androidx.sqlite.db.SupportSQLiteDatabase
//import com.example.featuresdk.R
//
//import com.example.featuresdk.model.Contact
//import com.example.featuresdk.model.*
//import kotlinx.coroutines.CoroutineScope
//import kotlinx.coroutines.launch
//import java.sql.Timestamp
//
//class AppDatabaseFactory {
//
//    private class DemoDatabaseCallback(private val scope: CoroutineScope) : RoomDatabase.Callback() {
//
//        override fun onCreate(db: SupportSQLiteDatabase) {
//            super.onCreate(db)
//
//            INSTANCE?.let { demoDatabase ->
//                scope.launch {
//                    populateDatabase(demoDatabase)
//                }
//            }
//        }
//
//        //initialize database with default data
//        suspend fun populateDatabase(demoDatabase: AppDatabase) {
//            val bookDao = demoDatabase.bookDao()
//            bookDao.insert(Book("A-2021","Android","100"), Book("B-2021","Java","199"))
//
//            //initialize users
//            val users = mutableListOf<User>().apply {
//                add(User.ME)
//                var userId = User.ME.id!! + 1
//                addAll(IconManager.icons.map { User(userId++,it.key,it.value) })
//            }
//            demoDatabase.userDao().insert(users)
//
//            //initialize contacts
//            val contacts = mutableListOf<com.example.featuresdk.model.Contact>().apply {
//                addAll(users.subList(1,5).map {
//                    com.example.featuresdk.model.Contact(User.ME.id!!, it.id!!)
//                })
//                addAll(users.subList(5,10).map {
//                    com.example.featuresdk.model.Contact(
//                        User.ME.id!!,
//                        it.id!!,
//                        name = "${it.nickname}"
//                    )
//                })
//            }
//            demoDatabase.contactDao().insert(contacts)
//
//            //initialize topics
//            val topics = mutableListOf<Topic>().apply {
//                //create a personal topic for each contact using the user id as topic id
//                addAll(users.subList(1,10).map {
//                    Topic(it.id!!,it.nickname,TopicCategory.Personal, logo = it.icon)
//                })
//
//                //create 2 group topics
//                add(Topic(10000,"hot topic", TopicCategory.Group, logo = R.drawable.ic_teradata))
//                add(Topic(10001,"great news", TopicCategory.Group, logo = R.drawable.ic_firebird))
//            }
//            val topicDao = demoDatabase.topicDao()
//            topicDao.insert(topics)
//
//            //initialize topic participants
//            val topicParticipants = mutableListOf<com.example.featuresdk.model.TopicParticipant>().apply {
//                addAll(users.subList(1,10).map {
//                    com.example.featuresdk.model.TopicParticipant(it.id!!, it.id, it.nickname)
//                })
//
//                addAll(users.subList(8,15).map {
//                    com.example.featuresdk.model.TopicParticipant(it.id!!, 10000, it.nickname)
//                })
//
//                addAll(users.subList(4,12).map {
//                    com.example.featuresdk.model.TopicParticipant(it.id!!, 10001, it.nickname)
//                })
//            }
//            demoDatabase.topicParticipantDao().insert(topicParticipants)
//
//            //initialize some messages for some personal topics
//            val currentTimeMillis = System.currentTimeMillis() - 20000
//            val messageDao = demoDatabase.messageDao()
//
//            //initialize messages for some personal topics
//            val p1Messages = topicParticipants.filter { it -> it.userId < 5L }.map {
//                Message(null, it.topicId,it.userId,"hi, personal greeting from ${it.username}",null, MessageStatus.New,Timestamp(currentTimeMillis))
//            }
//            messageDao.insert(p1Messages)
//            p1Messages.forEach {
//                topicDao.updateNewCount(it.topicId)
//                topicDao.update(it.topicId,it.text?:"", it.timestamp)
//            }
//
//            //initialize messages for group1
//            val group1Messages = topicParticipants.filter { it -> it.topicId == 10000L }.map {
//                Message(null, it.topicId,it.userId,"hi, greeting from ${it.username}",null, MessageStatus.New,Timestamp(currentTimeMillis))
//            }
//            messageDao.insert(group1Messages)
//            topicDao.updateNewCount(10000L)
//            group1Messages.last().apply {
//                topicDao.update(this.topicId,this.text?:"", this.timestamp)
//            }
//
//            messageDao.insert(Message(null, 5,User.ME.id!!,"Hi, how are you?",null, MessageStatus.Read,Timestamp(currentTimeMillis - 5000)))
//            messageDao.insert(Message(null, 5,5,"Great, thanks!",null, MessageStatus.New,Timestamp(currentTimeMillis + 5000)))
//            topicDao.update(5,"Great, thanks!", Timestamp(currentTimeMillis + 5000))
//            topicDao.updateNewCount(5L)
//        }
//    }
//
////    private class DemoQueryCallback : RoomDatabase.QueryCallback {
////        override fun onQuery(sqlQuery: String, bindArgs: MutableList<Any>) {
////            //println("QueryCallback: sqlQuery=${sqlQuery}")
////        }
////    }
//
//    companion object {
//        //Singleton prevents multiple instances of database opening at the same time.
//        @Volatile
//        private var INSTANCE : AppDatabase? = null
//
//        fun createDatabase(context: Context, scope: CoroutineScope): AppDatabase {
//            return INSTANCE ?: synchronized(this){
////                val instance = Room.databaseBuilder(context.applicationContext, AppDatabase::class.java,"demo_database.db")
////                    .addCallback(DemoDatabaseCallback(scope))
////                    .build()
//
//                val instance = Room.inMemoryDatabaseBuilder(context.applicationContext, AppDatabase::class.java)
//                    .addCallback(DemoDatabaseCallback(scope))
//                    .build()
//
//                INSTANCE = instance
//
//                instance
//            }
//        }
//    }
//}